package com.spring.cloud.study.user.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.spring.cloud.study.common.entity.SysMenu;
import com.spring.cloud.study.user.dao.SysMenuDao;
import com.spring.cloud.study.user.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/3/7
 * Time: 14:10
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao,SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuDao sysMenuDao;

    @Override
    public List<SysMenu> findMenuByRoleIds(List<Integer> roleIds) {
        return sysMenuDao.findMenuByRoleIds(roleIds);
    }
    /*


    @Autowired
    private SysMenuRepository sysMenuRepository;

    @Override
    public void save(SysMenu sysMenu) {
        SysMenu save = sysMenuRepository.save(sysMenu);
        sysMenuRepository.saveRoleMenu(save.getId(), Constant.SUPER_ADMIN_ROLE);
    }

    @Override
    public void saveBatch(Collection<SysMenu> collection) {

    }


    @Override
    public Page<SysMenu> findPage(Map<String,Object> params) {
        Specification specification =null;
        if(params.get("filter") != null && !params.get("filter").equals("")){
            String filter = params.get("filter").toString();
            JSONObject jsonObject = JSONObject.parseObject(filter);
            specification = new Specification<SysMenu>() {
                @Override
                public Predicate toPredicate(Root<SysMenu> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                    List<Predicate> predicates = new ArrayList<>();
                    if(jsonObject.get("name") != null && !jsonObject.get("name").equals("")){
                        predicates.add(criteriaBuilder.like(root.get("name"),"%"+jsonObject.get("name").toString()+"%"));
                    }
                    if(jsonObject.get("type") != null && !jsonObject.get("type").equals("")){
                        predicates.add(criteriaBuilder.equal(root.get("type"),jsonObject.get("type").toString()));
                    }
                    if(jsonObject.get("path") != null && !jsonObject.get("path").equals("")){
                        predicates.add(criteriaBuilder.like(root.get("path"),"%"+jsonObject.get("path").toString()+"%"));
                    }
                    if(jsonObject.get("perms") != null && !jsonObject.get("perms").equals("")){
                        predicates.add(criteriaBuilder.like(root.get("perms"),"%"+jsonObject.get("perms").toString()+"%"));
                    }
                    return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            };
        }
        Sort sort = new Sort(Sort.Direction.ASC, "id");
        Pageable pageable = new PageRequest(Integer.parseInt(params.get("page").toString())-1, Integer.parseInt(params.get("limit").toString()), sort);
        Page<SysMenu> page =null;
        if(specification != null){
            page = sysMenuRepository.findAll(specification,pageable);
        }else{
            page = sysMenuRepository.findAll(pageable);
        }

        return page;
    }



    @Override
    public List<SysMenu> findBatch(Object[] objects) {
        return null;
    }

    @Override
    public List<SysMenu> findBatch(Collection<Integer> collection) {
        List<SysMenu> sysMenus = sysMenuRepository.findAll(collection);
        return sysMenus;
    }

    @Override
    public SysMenu findById(int id) {
        return null;
    }

    @Override
    public List<SysMenu> findAll() {
        List<SysMenu> sysMenus = sysMenuRepository.findAll();
        return sysMenus;
    }

    @Override
    public void update(SysMenu sysMenu) {
        SysMenu save = sysMenuRepository.save(sysMenu);
    }

    @Override
    public int delete(SysMenu sysMenu) {
        return 0;
    }

    @Override
    public void delete(int id) {
        sysMenuRepository.delete(id);
    }


    @Override
    public int deleteBatch(Object[] objects) {
        return 0;
    }

    @Override
    public void deleteBatch(Collection<Integer> collection) {
       // String ids = StringUtils.join(collection.toArray(), ",");
        sysMenuRepository.deleteRoleMenu(collection);
        sysMenuRepository.deleteBatchById(collection);

    }

    @Override
    public List<SysMenu> findMenuByUserName(String userName) {
        List<SysMenu> menuByUserName = sysMenuRepository.findMenuByUserName(userName);
        return menuByUserName;
    }*/

    /**
     * 根据用户名得到用户的菜单权限，不包含按钮
     * @param userName
     * @return
     */
/*

    @Override
    public List<SysMenu> getUserMenuPremission(String userName) {
        List<SysMenu> sysMenus = sysMenuRepository.findMenuByUserName(userName);
*/
/*

        Map<Integer, List<SysMenu>> collect = sysMenus.parallelStream()
                .collect(Collectors.groupingBy(SysMenu::getParentId));

        List<SysMenu> sysMenuIsDir = sysMenus.parallelStream()
                .filter(sysMenu -> sysMenu.getParentId() == 0).collect(Collectors.toList());

*//*


        return generateMenuTree(sysMenus);
    }
*/

/*

    @Override
    public List<SysMenu> findByParentId(int parentId) {

        return sysMenuRepository.findByParentIdIs(parentId);
    }

    @Override
    public List<SysMenu> findByTypeIs(int type) {
        List<SysMenu> sysMenus = sysMenuRepository.findByTypeIs(type);
        return sysMenus;
    }

    @Override
    public List<SysMenu> getUserAllPremissionTreeList(String userName) {
        List<SysMenu> sysMenus = findMenuByUserName(userName);
        Map<Integer, List<SysMenu>> collect = sysMenus.parallelStream()
                .collect(Collectors.groupingBy(SysMenu::getParentId));

        List<SysMenu> collect1 = sysMenus.parallelStream()
                .filter(sysMenu -> sysMenu.getParentId() == 0).collect(Collectors.toList());

        List<SysMenu> menus = setMenuTreeList(collect1,collect);

        return menus;
    }

    @Override
    public List<SysMenu> getMenuTreeByRoleId(int roleId) {
        List<SysMenu> sysMenus = sysMenuRepository.findMenuByRoleId(roleId);
        Map<Integer, List<SysMenu>> collect = sysMenus.parallelStream()
                .collect(Collectors.groupingBy(SysMenu::getParentId));

        List<SysMenu> collect1 = sysMenus.parallelStream()
                .filter(sysMenu -> sysMenu.getType().equals("0")).collect(Collectors.toList());

        List<SysMenu> menus = setMenuTreeList(collect1,collect);
        return menus;
    }



    @Override
    public void deleteBatchById(List<Integer> ids) {
      //  String join = StringUtils.join(ids.toArray(), ",");
        sysMenuRepository.deleteBatchById(ids);
    }


    @Override
    public List<SysMenu> setMenuTreeList(List<SysMenu> sysMenus,Map<Integer, List<SysMenu>> collect){
        List<SysMenu> subMenuList = new ArrayList<>();
        for(SysMenu entity : sysMenus){
            if(!entity.getType().equals("2")){
                List<SysMenu> childMenus = collect.get(entity.getId());
                if(collect.get(entity.getId()) != null){
                    childMenus.parallelStream().forEach(sysMenu -> sysMenu.setParentName(entity.getName()));
                    entity.setChildren(setMenuTreeList(childMenus,collect));
                    subMenuList.add(entity);
                }
            }
        }
        return sysMenus;
    }

    @Override
    public List<SysMenu> generateMenuTree(List<SysMenu> sysMenus) {
        Map<Integer, List<SysMenu>> collect = sysMenus.parallelStream()
                .collect(Collectors.groupingBy(SysMenu::getParentId));

        List<SysMenu> sysMenuIsDir = sysMenus.parallelStream()
                .filter(sysMenu -> sysMenu.getParentId() == 0).collect(Collectors.toList());

        return getUserMenuNavList(sysMenuIsDir,collect);
    }


    private List<SysMenu> getUserMenuNavList(List<SysMenu> menuList, Map<Integer, List<SysMenu>> collect){
        List<SysMenu> subMenuList = new ArrayList<>();
        for(SysMenu entity : menuList){
            //目录
            if(entity.getType().equals("0")){
                List<SysMenu> childMenus = collect.get(entity.getId());
                if(childMenus != null && childMenus.size() > 0) {
                    childMenus.parallelStream().forEach(sysMenu -> sysMenu.setParentName(entity.getName()));
                    entity.setChildren(getUserMenuNavList(childMenus,collect));

                }

           }
            subMenuList.add(entity);
        }

        return subMenuList;
    }*/
}
